/*
 * Copyright (c) 2021 by zhongfu.net
 * @Author: xuning
 * @Email: xuning@zhongfu.net
 */

package org.gokeep.application.config;

import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.gokeep.common.config.properties.CacheProperties;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.SerializationCodec;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import java.util.Arrays;

/**
 * 这里和二级缓存公用一套配置
 * 默认是不检查redis配置，如果想用redis。则必须配置好redis配置信息
 * 配置文件：src/main/resources/redisson-*.yml
 * @author Administrator
 */
@Configuration
public class RedissonConfig {

    @Resource
    private CacheProperties cacheProperties;

    @Bean
    @Lazy
    public RedissonClient redissonClient() {
        return createRedissonClient();
    }

    private RedissonClient createRedissonClient() {
        Config config = generateConfig();
        if (!cacheProperties.getRedisson().isUseSingleServer()) {
            generateClusterServersConfig(config);
        }else {
            generateSingleServerConfig(config);
        }
        return Redisson.create(config);
    }

    private Config generateConfig() {
        Config config =  new Config();
        config.setThreads(8);
        config.setCodec(new SerializationCodec());
        return config;
    }

    private void generateClusterServersConfig(Config config) {
        ClusterServersConfig clusterServersConfig = config.useClusterServers();
        clusterServersConfig.setNodeAddresses(Arrays.asList(cacheProperties.getRedisson().getAddress()));
        clusterServersConfig.setPassword(cacheProperties.getRedisson().getPassword());
        clusterServersConfig.setIdleConnectionTimeout(5000);
        clusterServersConfig.setConnectTimeout(2000);
        clusterServersConfig.setRetryAttempts(1);
        clusterServersConfig.setRetryInterval(2000);
        clusterServersConfig.setSubscriptionsPerConnection(5);
        clusterServersConfig.setSubscriptionConnectionMinimumIdleSize(1);
        clusterServersConfig.setMasterConnectionPoolSize(2);
        clusterServersConfig.setSlaveConnectionPoolSize(2);
    }

    private void generateSingleServerConfig(Config config) {
        SingleServerConfig singleServerConfig = config.useSingleServer();
        singleServerConfig.setAddress(cacheProperties.getRedisson().getAddress());
        String password = cacheProperties.getRedisson().getPassword();
        if (StringUtils.isBlank(password)) {
            singleServerConfig.setPassword(null);
        }else {
            singleServerConfig.setPassword(password);
        }
        singleServerConfig.setIdleConnectionTimeout(5000);
        singleServerConfig.setConnectTimeout(2000);
        singleServerConfig.setRetryAttempts(1);
        singleServerConfig.setRetryInterval(2000);
        singleServerConfig.setSubscriptionsPerConnection(5);
        singleServerConfig.setSubscriptionConnectionMinimumIdleSize(1);
        singleServerConfig.setConnectionMinimumIdleSize(2);
        singleServerConfig.setConnectionPoolSize(8);
        singleServerConfig.setDatabase(0);
    }

}
